package com.huawei.spare.part.replace.views.widgets;

import com.github.daytron.simpledialogfx.dialog.Dialog;
import com.github.daytron.simpledialogfx.dialog.DialogType;
import com.huawei.spare.part.replace.OperationException;
import com.huawei.spare.part.replace.framework.SmartDataBus;

public class AlertDialog {

    public static String i18n(String key) {
        return SmartDataBus.getString(key);
    }

    public static void info(String title, String content) {
        Dialog dialog = new Dialog(DialogType.INFORMATION, i18n(title), i18n(content));
        dialog.setAlwaysOnTop(true);
        dialog.showAndWait();
    }

    public static Dialog confirm(String title, String content) {
        Dialog dialog = new Dialog(DialogType.CONFIRMATION, i18n(title), i18n(content));
        dialog.setAlwaysOnTop(true);
        dialog.showAndWait();
        return dialog;
    }

    public static void error(String title, String content) {
        Dialog dialog = new Dialog(DialogType.ERROR, i18n(title), i18n(content));
        dialog.setAlwaysOnTop(true);
        dialog.showAndWait();
    }

    public static void operationFailed(OperationException ex) {
        Dialog dialog = new Dialog(DialogType.ERROR, i18n(ex.getMajor()), i18n(ex.getDetails()));
        dialog.setAlwaysOnTop(true);
        dialog.showAndWait();
    }

    public static void error(Throwable e) {
        if (e instanceof OperationException) {
            AlertDialog.operationFailed((OperationException) e);
        } else {
            final Dialog dialog = new Dialog((Exception) e);
            dialog.setAlwaysOnTop(true);
            dialog.showAndWait();
        }
    }

    public static void error(Throwable ex, String title) {
        Dialog dialog = new Dialog(title, (Exception) ex);
        dialog.setAlwaysOnTop(true);
        dialog.showAndWait();
    }

}
